<!doctype html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=0, width=device-width"/>
        <meta name="format-detection" content="telephone=no"/>
        <title>是否发电详情</title>
        <link rel="stylesheet" type="text/css" href="../../css/aui.css"/>
        <link rel="stylesheet" type="text/css" href="../../css/api.css" />
        <link rel="stylesheet" type="text/css" href="../../css/common.css" />
        <link rel="stylesheet" type="text/css" href="css/billElectric.css" />
        <style>
            .cardCon *{
                font-size:14px;
            }
            .saomabtn{
              width: 100px;
              text-align: center;
              font-size: 14px;
              background-color: #3779d0;
              border: none;
              color: #fff;
              display: inline-block;
              border-radius: 3px;
              padding:5px 0;
              margin-top: 4px;
            }
            .cardCon2 {
          			width: 60%;
          			float: left;
          			border-radius: 4px;
          			overflow: hidden;
          			margin-right: 0px;
          			margin-top: 10px;
          	}
          	.cardDiv {
          	    width: 100%;
          	    font-size: 14px;
          	    margin-top: 10px;
          			overflow: hidden;
          	}
            .removebtn{
        			position: absolute;
        			right: 0;
        			top: 10px;
        			width: 20px!important;
        			height: 20px;
        			padding: 0;
              margin: 0;
        		}
        </style>
    </head>
    <body>
      <script id="img-template" type="text/x-dot-template">
          <li class="aui-list-view-cell aui-img aui-col-xs-4">
          <img class="aui-img-object" src="{{=it.imgpath}}"  onclick="imageUtil.show('{{=it.imgpath}}')">
          <button class="aui-btn aui-btn-danger removebtn" onclick="removeImg('{{=it.imgpath}}');">X</button>
          </li>
      </script>
        <div id="wrap">
            <div style="overflow:auto;margin-bottom:55px;">
            	 <div class="cardDiv">
                   <div class="cardLabel">
                       是否购买发电：
                   </div>
                    <div class="cardCon">
                      <span id="payElecTdId" style="line-height:40px;"></span>
                        <!-- <input id="payElecTdId" type="text"  readonly="readonly" style="background-color:white;border:solid 1px #E0E0E0;width:100%;height:100%;"> -->
                    </div>
                </div>
                 <div id="isele_id" class="cardDiv" style="display: none">
                    <div class="cardLabelDiv">
                        <div class="cardLabelTop">
                            运营商是否&nbsp;&nbsp;&nbsp;&nbsp;
                        </div>
                        <div class="cardLabelDown">
                            同意发电：
                        </div>
                    </div>
                    <div class="cardCon">
                        <select id="agreeElectri" onchange="doJluBill('#wrap')" name="agreeElectri" style="background-color:#FFFFFF;width:100%;height:100%; color: #db4537" value="">
                            <option value="Y">同意</option>
                            <option value="N">不同意</option>
                        </select>
                    </div>
                </div>

                <div id="isStandStaHidDivId" class="cardDiv">
                    <div class="cardLabel">
                        是否需要发电：
                    </div>
                    <div class="cardCon">
                        <select id="needElectric" name='needElectric' onchange="isNeedElectric()" style="background-color:#FFFFFF;width:100%;height:100%;color:#db4537" value="">
                            <option value="">请选择</option>
                            <option value="Y">是</option>
                            <option value="N">否</option>
                        </select>
                    </div>
                </div>


                <div id="wait_id" class="cardDiv" style="display: none">
                    <div class="cardLabelDiv">
                        <div class="cardLabelTop">
                            预设发电等待&nbsp;&nbsp;&nbsp;&nbsp;
                        </div>
                        <div class="cardLabelDown">
                            时长（分钟）：
                        </div>
                    </div>
                    <div class="cardCon">
                        <span id = "bookWaitTimeId" style="width:100%;height:100%;line-height:250%;padding:0 9px"></span>
                        <!--<input id="bookWaitTimeId" type="text"  readonly="readonly" style="background-color:white;border:solid 1px #E0E0E0;width:100%;height:100%;">-->
                    </div>
                </div>
                <div id="waitTimeDivId" class="cardDiv" style="display: none">
                    <div class="cardLabelDiv">
                        <div class="cardLabelTop">
                            实际发电等待&nbsp;&nbsp;&nbsp;&nbsp;
                        </div>
                        <div class="cardLabelDown">
                            时长（分钟）：
                        </div>
                    </div>
                    <div class="cardCon">
                        <input id="waitTimeId" onchange="doJluBill('#wrap')" type="number"  style="background-color:white;border:solid 1px #E0E0E0;width:100%;height:100%;">
                    </div>
                </div>
                <div class="cardDiv2">
                  <div class="cardLabel">
                    上传照片：
                  </div>
                  <div class="cardCon2">
                    <ul id="list-view" class="aui-list-view aui-grid-view">
                        <li id="addPic" class="aui-list-view-cell aui-img aui-col-xs-4" onclick="addPic(this)">
                            <img class="aui-img-object" src="../../image/icons/png/icon_addpic_focused.png">
                        </li>
                        <canvas style="display:none;">您的浏览器不支持Canvas</canvas>
                        <img class='imgPath' style="display:none;"  src=''/>
                    </ul>
                  </div>
                </div>
                <div class="cardTextareaDiv">
                    <div class="cardTextareaLabel">
                        备注：
                    </div>
                    <div class="cardTextareaCon">
                        <textarea id="dealCommentId" rows="8" name="dealCommentId" class="noborder" style="background-color:white;border:solid 1px green;width:100%;height:100%;" onchange="doJluBill('#wrap')"></textarea>
                    </div>
                </div>
            </div>
            <!--停电回单end-->
            <footer style="height:50px;">
                <div class="cardButtonDiv">
                    <button class="cancelBtn"  onclick="closeWin()">
                        取消
                    </button>
                    <button class="okBtn"  onclick="electricFunc();">
                        确定
                    </button>
                </div>
            </footer>
        </div>
    </body>
    <script src="../../script/jquery-1.11.3.min.js"></script>
    <script type="text/javascript" src="../../script/api.js"></script>
    <script type="text/javascript" src="../../script/doT.min.js"></script>
    <script type="text/javascript" src="../../script/common.js"></script>
    <script type="text/javascript" src="../../script/config.js"></script>
    <script type="text/javascript" src="../../script/client.api.js"></script>
    <script type="text/javascript" src="../../script/aui-alert.js"></script>
    <!-- wxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx -->
    <script type="text/javascript" src="../../script/cache.js"></script>
  	<script type="text/javascript" src="../../script/xunjian_db.js"></script>
    <script type="text/javascript" src="../../script/jquery-validate.js"></script>
    <script type="text/javascript" src="../../script/utils/ImageUtil.js"></script>

  	<!-- wxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx -->
    <script type="text/javascript" src="../../script/debug.js"></script>
    <script>

    	 var billModel;
       // wxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
       var imageUtil;
       // wxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        apiready = function() {
          db_init();

          imageUtil.init({
              uploadFn : $client.uploadBillImage,
              saveFn : db_saveBillPic
          });
          // wxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
             billModel = $api.getStorage('billModel');
            var header = initHeader();
            var thridHeader = $api.offset(header);
            api.pageParam.billElectric = true;

            if(billModel.whether_the_power==""){
            	$('#isele_id').show();
            	$('#wait_id').show();
            	$('#waitTimeDivId').show();

             if (billModel.st_waittime != null)
            var st_waittime = billModel.st_waittime;
            $('#bookWaitTimeId').text(st_waittime);
            $('#waitTimeId').val(st_waittime).css('color', 'red');
            }
            var billDetailInfo = $api.getStorage('billDetailInfo');
            var billDetailInfoList=billDetailInfo.list;
            // alert(JSON.stringify(billDetailInfo))
            var fdArr=[];
            if(billDetailInfoList.st_ifbuyeletric == "Y"){
              fdArr.push('移动')
            }
            if(billDetailInfoList.ltifbuyeletric == "Y"){
              fdArr.push('联通')
            }
            if(billDetailInfoList.dxifbuyeletric == "Y"){
              fdArr.push('电信')
            }
            if(fdArr.length>0){
              $('#payElecTdId').html(fdArr.join());
            }else {
              $('#payElecTdId').html('否');
            }

            api.setStatusBarStyle({
                style : 'light'
            });
            api.addEventListener({
                name : 'viewappear'
            }, function(ret, err) {
                api.closeFrame({
                    name : 'billMenu_frm'
                });
            });
            // 填充本地数据start
            doJluSetBill();
            setImgArr();
            // 填充本地数据end
        };
        //发电操作
        function electricFunc() {
            var billModel = $api.getStorage('billModel');
            var user = $api.getStorage('user');
            var userId = user.userid;
            var billSn = billModel.billsn;
            var billId = billModel.billid;
            var taskId = billModel.taskid;
            var actionType = $api.byId('needElectric').value;
            var agreeElectri = $api.byId('agreeElectri').value;
            var limitTime = $api.byId('waitTimeId').value;
            var dealComment = $api.byId('dealCommentId').value;
            if (actionType == "Y" && !checkRate(limitTime)&&billModel.whether_the_power=="") {
                api.toast({
                    msg : "实际发电等待的时长（分钟）必须填数字",
                    location : 'bottom'
                });
                return false;
            } else if (actionType == "N"&&dealComment=="") {
               api.toast({
                    msg : "不需要发电，备注必填",
                    location : 'bottom'
                });
                return false;
            } else if (actionType == "") {
                api.toast({
                    msg : "请判断是否需要发电",
                    location : 'button'
                });
                return false;
            }
            api.showProgress({
                title : '处理中',
                modal : false
            });
//          billSn, actionType, limitTime, dealComment, agreeElectri, billId,taskId
			var data = {};
			data.billSn=billSn;
			data.actionType=actionType;
			data.dealComment=dealComment;
			data.billId=billId;
			data.taskId=taskId;
			if(billModel.whether_the_power==""){
			data.limitTime=limitTime;
			data.agressBuy=agreeElectri;
			}
            $client.electrictJudge(data, function(ret, err) {
                if (ret) {
                    if (true) {
                      if (imageUtil.getImageArray().length) {
            						imageUtil.upload(function(count) {
                          window.setTimeout(function() {
                          api.execScript({
                            name : 'bill-list_win',
                            script : 'refreshBillList('+1+');'
                          });
                          api.closeToWin({
                              name : 'bill-list_win'
                            });
                          }, 1100);
            						});
            					}else {
                        if (actionType == "Y") {
                            api.toast({
                                msg : "发电成功",
                                duration : 1000,
                                location : 'middle'
                            });
                        } else {
                            api.toast({
                                msg : "操作成功",
                                duration : 1000,
                                location : 'middle'
                            });
                        }
                        window.setTimeout(function() {
                        api.execScript({
                          name : 'bill-list_win',
                          script : 'refreshBillList('+1+');'
                        });
                        api.closeToWin({
                            name : 'bill-list_win'
                          });
                        }, 1100);
            					}

                    } else {
                        api.toast({
                            msg : ret.data_info,
                            location : 'middle'
                        });
                    }
                } else {
                    api.toast({
                        msg : err.msg,
                        location : 'middle'
                    });
                }
                api.hideProgress();
            });
        }

        function isNeedElectric() {

        	  if(billModel.whether_the_power==""){
            var needElectricVal = document.getElementById('needElectric').value;
            var waitTimeDivObj = document.getElementById('waitTimeDivId');
            var waitTimeObj = document.getElementById('waitTimeId');
            //重置发电时长值
            waitTimeObj.value = "";
            if (needElectricVal == "Y") {
                waitTimeDivObj.style.display = "block";
            } else {
                waitTimeDivObj.style.display = "none";
            }
            }
            doJluBill('#wrap')
        }

        function closeWin() {
            api.closeWin({
                name : 'billElectric_win'
            });
        }
        /**
    		 * 添加图片// wxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    		 */
    		function addPic(e) {
         		var permission = 'camera';
				var resultList = api.hasPermission({
				    list: [permission]
				});
				if (resultList[0].granted) {
				    // 已授权，可以继续下一步操作
				} else {
				    api.confirm({
				        msg: '应用需要您的授权才能访问相机',
				        buttons: ['取消', '去设置']
				    }, function(ret) {
				        if (ret.buttonIndex == 2) {
				            api.requestPermission({
				                list: [permission],
				            }, function(res) {
				                if (res.list[0].granted) {
				                    // 已授权，可以继续下一步操作
				                }
				            });
				        }
				    });
				}
    			var lastLocation = $api.getStorage('LAST_GPS_LOCATION');
    			var lon1 = displaynum(lastLocation.lon);
    			var lat1 = displaynum(lastLocation.lat);
    			if (!lastLocation || !lastLocation.status) {
    				api.toast({
    					msg : '暂未定位\n无法上传照片！'
    				});
    			}else {
    				////////////////////////////////////////////
    				var curentTime2 = CurentTime2();
    				$api.setStorage('newtime', curentTime2);
    				$api.setStorage('lon1', lon1);
    				$api.setStorage('lat1', lat1);
    				var lon1 = $api.getStorage('lon1');
    				var lat1 = $api.getStorage('lat1');
    				var user=$api.getStorage('user');
    				var newtime = $api.getStorage('newtime');
    				var lon1 = $api.getStorage('lon1');
    				var lat1 = $api.getStorage('lat1');
    				var billModel = $api.getStorage('billModel');
    				////////////////////////////////////////////


    				imageUtil.add('camera', function(ret, err) {

    					var stationname=api.pageParam.stationname;
    					var billId=api.pageParam.billId;
    					var billSn=api.pageParam.billSn;
    					var businessid = billModel.businesstype;

    						// wxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    						// var oldret = ret.data.split(".");
    						// var newret = oldret[0]+"_shuiyin;"+user.userid+";"+user.userid+";"+billId+';'+businessid+';'+billSn+';'+lon1+";"+lat1+";"+lastLocation.photoaddress+";"+newtime+";."+oldret[1];
    						// console.log(newret);
    						// wxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    						// var filePath = ret.data;
    						var imgModel = {
    								userid : user.userid,
    								billid : api.pageParam.billId,
    								businessid : billModel.businesstype,
    								beiyong:"",
    								stationname : billModel.stationname,
    								billsn : billModel.billsn,
    								imgpath : ret,
    								phototime :(new Date()).pattern("yyyy-MM-dd HH:mm:ss"),
    								lon : lon1,
    								lat : lat1,
    								photoaddress : lastLocation.photoaddress,
                    state:api.pageParam.billStatus,
                    billstatusname:billModel.billstatusname
    						};
                // wxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                cacheImgArr(imgModel);
                // wxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                console.log(getObj(imgModel));
    						imageUtil.getImageArray().push(imgModel);
    					showlog('添加一张待上传图片,待上传图片数组长度:' + imageUtil.getImageArray().length);
    					var tpl = $api.byId('img-template').text;
    					var tempFn = doT.template(tpl);

    							$('#addPic').before(tempFn(imgModel));
    	            $new = $('#addPic').prev();
    	            $new.css('height', $new.css('width'));

    				},e,true,false);
    			}
    		}

    			/**
    			 *删除图片
    			 */
    			function removeImg(imgPath) {
    				var imageArray = imageUtil.getImageArray();
    				var size = imageArray.length;
    				for (var i = 0; i < size; i++) {
    					var data = imageArray[i];
    					if (data.imgpath == imgPath) {
    						imageArray.remove(i);
    						break;
    					}
    				}
    				$(event.target).parents('.picBox').find('#add').show();
    				$(event.target).parents('li.aui-img').remove();
            // 删除本地缓存图片其一
            deleteImgArr(imgPath);
    			}

    			// wxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    </script>
</html>
